<?php
declare(strict_types=1);
/**
 * Created by project.
 * User: huanzipap
 * Date: 2023/5/10
 * Time: 11:25
 */

namespace App\Controller\Admin;

use App\Controller\AppBaseController;
use App\Model\Act;
use App\Model\ActPrizeGoods;
use App\Model\ActPrizeJoin;
use Hyperf\HttpServer\Annotation\Controller;
use Hyperf\HttpServer\Annotation\RequestMapping;
use Hyperf\HttpServer\Annotation\Middleware;
use App\Middleware\AdminApiAuth;

#[Controller(prefix: 'admin')]
#[Middleware(AdminApiAuth::class)]
class ActPrizeController extends AppBaseController
{

    /**
     * 后台--活动抽奖奖品列表
     * @param int $act_id
     * @return object
     */
    #[RequestMapping(path: 'prizeGoodsList/{act_id}',methods: 'get')]
    public function prizeGoodsList(int $act_id) :object
    {
        try{
            //验证管理员信息
            $checkInfo = $this->checkAdmin();
            if($checkInfo['status']==40002) return $this->reMsg->re(10002,$checkInfo['message']);

            $where['act_id'] = $act_id;
            $list = ActPrizeGoods::where($where)
                ->selectRaw('id,act_id,type,name,status')
                ->orderByDesc('sort')
                ->orderByDesc('id')
                ->get();
            $total = 0;
            $next = 0;
            return $this->reMsg->re(1,'success',['list' => $list,'next' => $next,'total' => $total]);
        }catch (\Exception $exception){
            return $this->diyError->errorJson($exception);
        }
    }


    /**
     * 后台--活动抽奖奖品|详情
     * @param int $prize_goods_id
     * @return object
     */
    #[RequestMapping(path: 'prizeGoodsInfo/{prize_goods_id}',methods: 'get')]
    public function prizeGoodsInfo(int $prize_goods_id) :object
    {
        try{
            //验证管理员信息
            $checkInfo = $this->checkAdmin();
            if($checkInfo['status']==40002) return $this->reMsg->re(10002,$checkInfo['message']);

            $info = ActPrizeGoods::where('id',$prize_goods_id)->first();
            if(empty($info)) return $this->reMsg->re(10008);
            return $this->reMsg->re(1,'success',$info);
        }catch (\Exception $exception){
            return $this->diyError->errorJson($exception);
        }
    }

    /**
     * 后台--活动抽奖商品|添加编辑
     * @param int|null $prize_goods_id
     * @return object
     */
    #[RequestMapping(path:'prizeGoodsEdit[/{prize_goods_id}]',methods: 'post,get')]
    public function prizeGoodsEdit(int|null $prize_goods_id): object
    {
        try{
            //验证管理员信息
            $checkInfo = $this->checkAdmin();
            if($checkInfo['status']==40002) return $this->reMsg->re(10002,$checkInfo['message']);
            $_admin = $checkInfo['data'];

            $data = $this->request->all();

            //虚拟数据
//            if(empty($data)){
//                $data['act_id'] = 3;
//                $data['type'] = 1;
//                $data['name'] = '特殊奖品';
//                $data['stock'] = 10;
//                $data['sort'] = 1;
//                $data['status'] = 1;
//            }
            //虚拟数据

            if(empty($data['type']) || !in_array($data['type'],[1,2])
                || empty($data['name'])
            ) return $this->reMsg->re(10007);

            if(empty($data['stock']) || !is_numeric($data['stock'])){
                return $this->reMsg->re(0,'库存必须要数字大于0');
            }

            if(empty($data['sort']) || !is_numeric($data['sort'])){
                return $this->reMsg->re(0,'排序必须要数字大于0');
            }
            empty($data['status']) || !is_numeric($data['status']) ? $data['status'] = 0 : $data['status'] = 1;

            //活动信息
            $actInfo = Act::where('id',$data['act_id'])->first();
            //判断抽奖活动是否开始
            if(time()>strtotime($actInfo->start_time)){
                return $this->reMsg->re(0,'活动已开始不可操作');
            }

            $party_id = $actInfo->party_id; //所属支部

            if($prize_goods_id){
                //编辑
                $info = ActPrizeGoods::where('id',$prize_goods_id)->first();
                if(empty($info)) return $this->reMsg->re(10008);

                //判断是否有权操作
                if($_admin->level!=1 && $_admin->party_id!=$actInfo->party_id) return $this->reMsg->re(10009);

                $info->type = $data['type'];
                $info->name = $data['name'];
                $info->sort = $data['sort'];
                $info->stock = $data['stock'];
                $info->status = $data['status'];
                $info->party_id = $party_id;
                $info->admin_id = $_admin->id;
                $info->save();
                return $this->reMsg->re(1);
            }else{
                $data['party_id'] = $party_id;
                $data['admin_id'] = $_admin->id;
                $insertRet = ActPrizeGoods::create($data);
                if($insertRet){
                    return $this->reMsg->re(1);
                }else{
                    return $this->reMsg->re(0);
                }
            }
        }catch (\Exception $exception){
            return $this->diyError->errorJson($exception);
        }
    }


    /**
     * 后台--活动抽奖商品|删除
     * @param int $prize_goods_id
     * @return object
     */
    #[RequestMapping(path:'prizeGoodsDelete/{prize_goods_id}',methods: 'get')]
    public function prizeGoodsDelete(int $prize_goods_id): object
    {
        try{
            //验证管理员信息
            $checkInfo = $this->checkAdmin();
            if($checkInfo['status']==40002) return $this->reMsg->re(10002,$checkInfo['message']);
            $_admin = $checkInfo['data'];

            //判断信息
            $goodsInfo = ActPrizeGoods::where('id',$prize_goods_id)->first();
            if(empty($goodsInfo)) return $this->reMsg->re(10008);

            //活动信息
            $info = Act::where('id',$goodsInfo->act_id)->first();
            if(empty($actInfo)) return $this->reMsg->re(10008);
            //验证权限
            if($_admin->level!=1 && $_admin->party_id!=$info->party_id) return $this->reMsg->re(10009);

            //判断抽奖活动是否开始
            if(time()>strtotime($info->start_time)){
                return $this->reMsg->re(0,'已产生抽奖记录不可删除');
            }

            $goodsInfo->delete();
            return $this->reMsg->re(1);
        }catch (\Exception $exception){
            return $this->diyError->errorJson($exception);
        }
    }



    /**
     * 后台--活动抽奖参与人
     * @param int $act_id
     * @return object
     */
    #[RequestMapping(path: 'prizeJoinList/{act_id}',methods: 'get')]
    public function prizeJoinList(int $act_id) :object
    {
        try{
            //验证管理员信息
            $checkInfo = $this->checkAdmin();
            if($checkInfo['status']==40002) return $this->reMsg->re(10002,$checkInfo['message']);

            //先获取本次答题活动
            $info = Act::where('id',$act_id)->selectRaw('id,type')->first();
            if(empty($info)) return $this->reMsg->re(10008);
            if(!in_array($info->type,[3])) return $this->reMsg->re(0,'活动类型有误');

            $page = $this->request->input('page', 1);
            $limit = $this->request->input('limit', 20);
            $offset = ($page - 1) * $limit;

            $where['act_id'] = $act_id;
            $list = ActPrizeJoin::with(['user'=>function($query){
                    $query->selectRaw('id,nickname,avatar,auth_name')->get();
                }])
                ->where($where)
                ->orderByDesc('id')
                ->offset($offset)
                ->limit($limit)
                ->get();

            $total = 0;
            $next = 0;
            if($list->isNotEmpty()){
                if($page==1) $total = ActPrizeJoin::where($where)->count();
                if(count($list)>$limit) $next = 1;
            }
            return $this->reMsg->re(1,'success',['list' => $list,'next' => $next,'total' => $total]);
        }catch (\Exception $exception){
            return $this->diyError->errorJson($exception);
        }
    }

}